<?php
/**
 * 页面导航
 */
class IndexController extends Controller
{
    /**
     * 构造函数
     *
     * @return [type] [description]
     */
    public function run()
    {
        $this->Render("index");
    }
    /**
     * 添加新的记录
     */
    public function add()
    {
        $data = $this->input->Post('msg');
        $msg  = json_decode($data, true);
        $p1   = $msg['p1'];
        $p2   = $msg['p2'];
        $win  = $msg['win'];
        $tmp  = array_merge($p1, $p2, array("win" => $win));
        $f    = true;
        foreach ($tmp as $value) {
            $f = $f && $value;
        }
        if ($f) {
            $h1    = "" . $p1['skill'] . $p1['status'];
            $h2    = "" . $p2['skill'] . $p2['status'];
            $win   = $win == "p1" ? $h1 : $h2;
            $horse = new Horse();
            $flag  = $horse->add($h1, $h2, $win);
            if ($flag) {
                echo Tools::Ret(1, "添加成功", '');
            } else {
                echo Tools::Ret(-2, "系统错误", '');
            }
        } else {
            echo Tools::Ret(-1, "没选全", '');
        }
    }

    public function jisuan()
    {
    	$data = $this->input->Post('msg');
        $msg  = json_decode($data, true);
        $p1   = $msg['p1'];
        $p2   = $msg['p2'];
        $tmp  = array_merge($p1, $p2);
        $f    = true;
        foreach ($tmp as $value) {
            $f = $f && $value;
        }
        if ($f) {
            $h1    = "" . $p1['skill'] . $p1['status'];
            $h2    = "" . $p2['skill'] . $p2['status'];
            $horse = new Horse();
            $flag  = $horse->jisuan($h1, $h2);
            if ($flag) {
                echo Tools::Ret(1, "第一匹马的胜率为".$flag[0].", 第二匹马的胜率为".$flag[1], $flag);
            } else {
                echo Tools::Ret(-2, "没有记录", '');
            }
        } else {
            echo Tools::Ret(-1, "没选全", '');
        }
    }

}
